﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsPlatform.PermissionManagement.IPermissionManagement;
using SportsPlatform.Base;
using SportsPlatform.Consts;
using SportsPlatform.Query.PermissionManagement.CompanyTypeInfo;
using SportsPlatform.Model.PermissionManagement;
using SportsPlatform.Query.PermissionManagement.BaseQuery;
using SportsPlatform.Dto.PermissionManagement.CompanyTypeInfo;
using Abp.Domain.Repositories;
using Abp.Application.Services;
using Microsoft.AspNetCore.Http;
using SportsPlatform.Global;

namespace SportsPlatform.PermissionManagement.PermissionManagement
{
    /// <summary>
    /// 平台信息表
    /// </summary>
    [RemoteService(false)]  //隐藏自动生成的WebApi
    public class CompanyTypeInfoService :SportsPlatformAppServiceBase, ICompanyTypeInfoService
    {
        private readonly IRepository<CompanyTypeInfo, Guid> _companyTypeInfoRepository;

        private readonly IRepository<UserInfo, Guid> _userinfoRepository;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="companyTypeInfoRepository"></param>
        /// <param name="userinfoRepository"></param>
        public CompanyTypeInfoService(IRepository<CompanyTypeInfo, Guid> companyTypeInfoRepository, 
            IRepository<UserInfo, Guid> userinfoRepository, IHttpContextAccessor context) : base(context)
        {
            _companyTypeInfoRepository = companyTypeInfoRepository;
            _userinfoRepository = userinfoRepository;
        }
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="addCompanyTypeInfoQuery"></param>
        /// <returns></returns>
        public async Task<BaseResultDto> AddCompanyTypeInfo(AddCompanyTypeInfoQuery addCompanyTypeInfoQuery)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                var token = UldAdminSessionContext.GetCurrentUserInfo(_context);

                CompanyTypeInfo companyTypeInfo = ObjectMapper.Map<CompanyTypeInfo>(addCompanyTypeInfoQuery);
                companyTypeInfo.Id = Guid.NewGuid();
                companyTypeInfo.States = true;
                companyTypeInfo.CreateTime = DateTime.Now;
                companyTypeInfo.CreateUserId = token.Id;
                await _companyTypeInfoRepository.InsertAsync(companyTypeInfo);
                basePage.IsSuccess = true;
                basePage.Code = HttpResultCode.Success;
                basePage.Message = "新增成功！";
            }
            catch (Exception e)
            {
                basePage.IsSuccess = false;
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }
        /// <summary>
        /// 根据Id删除信息
        /// </summary>
        /// <param name="deleteOrQueryModel"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BaseResultDto> DeleteCompanyTypeInfo(DeleteOrGetModelQuery deleteOrQueryModel)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                var token = UldAdminSessionContext.GetCurrentUserInfo(_context);

                var companytypinfo = await _companyTypeInfoRepository.FirstOrDefaultAsync(info => info.Id == deleteOrQueryModel.Id);
                if (companytypinfo != null)
                {
                    companytypinfo.IsDeleted = true;
                    companytypinfo.DeleteUserId = token.Id;
                    await _companyTypeInfoRepository.UpdateAsync(companytypinfo);
                    basePage.IsSuccess=true;
                    basePage.Code = HttpResultCode.Success;
                }
                else {
                    basePage.IsSuccess = false;
                    basePage.Code = HttpResultCode.Fail;
                    basePage.Message = "未查询到要删除的数据！";
                }
            }
            catch (Exception e)
            {
                basePage.IsSuccess = false;
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }

        /// <summary>
        /// 根据Id获取信息
        /// </summary>
        /// <param name="deleteOrQueryModel"></param>
        /// <returns></returns>
        public async Task<ResultDto<GetCompanyTypeInfoDto>> GetCompanyTypeInfo(DeleteOrGetModelQuery deleteOrQueryModel)
        {
            ResultDto<GetCompanyTypeInfoDto> pageData = new ResultDto<GetCompanyTypeInfoDto>();
            try
            {
                var companytypeinfo = await _companyTypeInfoRepository.FirstOrDefaultAsync(info => info.Id == deleteOrQueryModel.Id);
                if (companytypeinfo != null)
                {
                    pageData.IsSuccess = true;
                    pageData.Code = HttpResultCode.Success;
                    pageData.Message = "查询成功！";
                    pageData.Data = ObjectMapper.Map<GetCompanyTypeInfoDto>(companytypeinfo);
                }
                else {
                    pageData.IsSuccess = false;
                    pageData.Code = HttpResultCode.Fail;
                    pageData.Message = "未查询到数据！";
                }
            }
            catch (Exception e)
            {
                pageData.IsSuccess = false;
                pageData.Code = HttpResultCode.Error;
                pageData.Message = e.Message.ToString();
            }
            return pageData;
        }

        /// <summary>
        /// 获取平台信息Llis
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultListDto<GetCompanyTypeInfoDto>> GetCompanyTypeInfoList()
        {
            ResultListDto<GetCompanyTypeInfoDto> resultListDto = new ResultListDto<GetCompanyTypeInfoDto>();
            try
            {
                var companytypeinfo = await _companyTypeInfoRepository.GetAllListAsync(info=>info.IsDeleted==false&&info.States==true);
                if (companytypeinfo != null && companytypeinfo.Count > 0)
                {
                    resultListDto.IsSuccess = true;
                    resultListDto.Code = HttpResultCode.Success;
                    resultListDto.Message = "查询成功！";
                    resultListDto.Data = ObjectMapper.Map<List<GetCompanyTypeInfoDto>>(companytypeinfo);
                }
                else
                {
                    resultListDto.IsSuccess = false;
                    resultListDto.Code = HttpResultCode.SqlError;
                    resultListDto.Message = "未查询到数据！";
                }
            }
            catch (Exception e)
            {
                resultListDto.IsSuccess = false;
                resultListDto.Code = HttpResultCode.Error;
                resultListDto.Message = e.Message.ToString();
            }
            return resultListDto;
        }
        /// <summary>
        /// 是否启用
        /// </summary>
        /// <param name="isStatesModel"></param>
        /// <returns></returns>
        public async Task<BaseResultDto> IsStatesCompanyTypeInfo(IsStatesdModel isStatesModel)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                var token = UldAdminSessionContext.GetCurrentUserInfo(_context);

                var companytypeinfo = await _companyTypeInfoRepository.FirstOrDefaultAsync(info => info.Id == isStatesModel.Id);
                if (companytypeinfo != null)
                {
                    companytypeinfo.UpdateTime = DateTime.Now;
                    companytypeinfo.UpdateUserId = token.Id;
                    companytypeinfo.States = isStatesModel.States;
                    await _companyTypeInfoRepository.UpdateAsync(companytypeinfo);
                    basePage.IsSuccess = true;
                    basePage.Code = HttpResultCode.Success;
                    basePage.Message = "更改成功！";
                }
                else
                {
                    basePage.IsSuccess = false;
                    basePage.Code = HttpResultCode.Fail;
                    basePage.Message = "未查询到数据！";
                }
            }
            catch (Exception e)
            {
                basePage.IsSuccess = false;
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="updateCompanyTypeInfoQuery"></param>
        /// <returns></returns>
        public async Task<BaseResultDto> UpdateCompanyTypeInfo(UpdateCompanyTypeInfoQuery updateCompanyTypeInfoQuery)
        {
            BaseResultDto basePage = new BaseResultDto();
            try
            {
                var token = UldAdminSessionContext.GetCurrentUserInfo(_context);

                var companytypeinfo = await _companyTypeInfoRepository.FirstOrDefaultAsync(info => info.Id == updateCompanyTypeInfoQuery.Id);
                if (companytypeinfo != null)
                {
                    if (!string.IsNullOrEmpty(updateCompanyTypeInfoQuery.CompanyTypeName))
                    {
                    companytypeinfo.CompanyTypeName = updateCompanyTypeInfoQuery.CompanyTypeName;
                    }
                    if (!string.IsNullOrEmpty(updateCompanyTypeInfoQuery.CompanyTypeDescribe))
                    {
                        companytypeinfo.CompanyTypeDescribe = updateCompanyTypeInfoQuery.CompanyTypeDescribe;
                    }
                    companytypeinfo.UpdateTime = DateTime.Now;
                    companytypeinfo.UpdateUserId = token.Id;
                    await _companyTypeInfoRepository.UpdateAsync(companytypeinfo);
                    basePage.IsSuccess = true;
                    basePage.Code = HttpResultCode.Success;
                    basePage.Message = "更新成功！";
                }
                else
                {
                    basePage.IsSuccess = false;
                    basePage.Code = HttpResultCode.Fail;
                    basePage.Message = "未查询到数据！";
                }
            }
            catch (Exception e)
            {
                basePage.IsSuccess = false;
                basePage.Code = HttpResultCode.Error;
                basePage.Message = e.Message.ToString();
            }
            return basePage;
        }
    }
}
